package com.tuanbiao.ui.auth;

import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.ViewUtils;
import com.hzjq.data.entity.PmEntity;
import com.hzjq.data.hzjq.entity.CapRecordEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.AuthController;
import com.tuanbiao.databinding.ActivityAuthOnlineBinding;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.AuthModel;
import com.tuanbiao.presenter.auth.AuthOnlinePresenter;
import com.tuanbiao.ui.auth.adapter.AuthOnlineAdapter;
import com.tuanbiao.ui.plan.PlanManageActivity;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.LibCollections;

import java.util.List;

public class AuthOnlineActivity extends BasePresenterActivity<AuthOnlinePresenter, ActivityAuthOnlineBinding> implements AuthController.OnlineView {
    private AuthOnlineAdapter authOnlineAdapter;
    private WaitDialog.Builder waitDialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, AuthOnlineActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityAuthOnlineBinding getBindingView() {
        return ActivityAuthOnlineBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.btnAddDetonator.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            PlanManageActivity.start(getActivity());
        });

        binding.btnDownloadWorkCode.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.downloadWorkCode(authOnlineAdapter.getData());
        });
        authOnlineAdapter = new AuthOnlineAdapter();
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvContent.setAdapter(authOnlineAdapter);
    }


    @Override
    public void showProject(PmEntity currentProject) {
        binding.tvProjectName.setText(currentProject.getPmName());
    }

    @Override
    public void setDetonatorItems(List<CapRecordEntity> entities) {
        if (LibCollections.isEmpty(entities)) {
            authOnlineAdapter.setEmptyView(ViewUtils.layoutId2View(R.layout.include_no_data));
        } else {
            int count = 0;
            for (CapRecordEntity entity : entities) {
                if (!entity.isAuth()) {
                    count++;
                }
            }
            if (count > 0) {
                showNoAuthDialog(count);
            }
            authOnlineAdapter.setNewInstance(entities);
        }
    }

    private void showNoAuthDialog(int count) {
        new MessageDialog.Builder(getActivity()).setTitle("提示")
                .setMessage("当前有" + count + "发雷管未授权！")
                .setAlone(true)
                .show();
    }

    @Override
    protected AuthOnlinePresenter createPresenter() {
        return new AuthOnlinePresenter(new AuthModel(), this);
    }

    @Override
    public void showLoading() {
        if (null == waitDialog) {
            waitDialog = new WaitDialog.Builder(getActivity())
                    .setCancelable(false)
                    .setCanceledOnTouchOutside(false)
                    .setMessage("正在授权");
        }
        waitDialog.show();
    }

    @Override
    public void hideLoading() {
        if (null != waitDialog) {
            waitDialog.dismiss();
        }
    }
}